package van.planifolia.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import van.planifolia.pojo.User;
import van.planifolia.result.ComResult;
import van.planifolia.service.UserService;

import javax.annotation.Resource;

/**
 * Created by Intellij IDEA<br>
 * User的控制层
 *
 * @author Planifolia.Van
 * @version 1.0
 * @date 2022/11/17 18:24
 */
@RestController
@RequestMapping("/user")
@Api("对用户表进行操作的api类")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 查询全部用户的控制器
     *
     * @param pageIndex 页面索引
     * @param pageSize  页面大小
     * @return 全部用户信息
     */
    @GetMapping("/all")
    @ApiOperation("获取全部用户的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize"
                    , value = "分页页面的大小"
                    , required = true
                    , dataType = "String"
            ),
            @ApiImplicitParam(name = "pageIndex"
                    , value = "当前的页码"
                    , required = true
                    , dataType = "String"
            )
    })
    public ComResult getAllUser(Integer pageSize, Integer pageIndex) {
        return new ComResult(200, "查询成功", userService.getAllUserMessage(pageSize, pageIndex));
    }

    /**
     * 查询单条用户
     *
     * @param id 用户的id
     * @return 单条用户信息
     */
    @GetMapping("/one")
    @ApiOperation("按照id获取用户信息")
    @ApiImplicitParam(
            name = "id",
            value = "用户id",
            required = true,
            dataType = "int"
    )
    public ComResult getOneUser(Integer id) {
        return new ComResult(200, "查询成功", userService.getUserById(id));
    }

    /**
     * 插入一条用户信息
     *
     * @param user 用户信息
     * @return 公共的结果集
     */
    @PostMapping("/add")
    @ApiOperation("添加一条用户信息")
    public ComResult addOneUser(@RequestBody User user) {
        System.out.println(userService.addUser(user));
        return new ComResult(200, "添加成功");
    }

    /**
     * 根据id删除用户
     *
     * @param id 被删除用户的id
     * @return 公共的结果集
     */
    @ApiOperation("按照id删除用户信息")
    @ApiImplicitParam(
            name = "id",
            value = "用户id",
            required = true,
            dataType = "int"
    )
    @DeleteMapping("/remove")
    public ComResult removeUser(Integer id) {
        System.out.println(userService.deleteUser(id));
        return new ComResult(200, "删除成功");
    }

    /**
     * 更新一条用户信息
     *
     * @param user 要更新的用户
     * @return 公共的结果集
     */
    @PutMapping("/modify")
    @ApiOperation(value = "按照id修改用户信息",notes = "123")
    public ComResult modifyUser(@RequestBody User user) {
        System.out.println(userService.updateUser(user));
        return new ComResult(200, "修改成功");
    }
}
